/**
  \mainpage
  ETFRoot project, Anyang Normal University && IMP-CAS
  \brief analyse the raw data, including data mapping, tracking and PID
  \author SUN Yazhou, asia.rabbit@163.com
  \since 2021-11-20
  \date 2021-12-04 last modified
  \attention
  changelog
  <table>
  <tr>  <th>Date         <th>Author      <th>Description                   </tr>
  <tr>  <td>2021-11-20   <td>Asia Sun    <td>file created                  </tr>
  </table>

  \copyright Copyright (c) 2021 Anyang Normal U. && IMP-CAS with LGPLv3 LICENSE
*/

void ana(){
  // VMCWORKDIR: path to ETFAna root directory -> used to indicate path to config files
  setenv("VMCWORKDIR", TString(get_current_dir_name())+"/../ETFAna", true);
  ETFMsg::SetDebug(); // display debug info

  static ETFParaManager *PARA = ETFParaManager::Instance();
  // PARA->SetPXIFile("/put/your/pxifile/here"); PARA->SetVMEFile("/put/your/vmefile/here");
  auto source = new ETFMixedOfflineSource(PARA->pxifile(), PARA->vmefile());
  FairRunOnline *run = new FairRunOnline(source);
  source->AddUnpacker(new ETFPXIUnpacker());
  source->AddUnpacker(new ETFUserVMEUnpacker());
  run->SetSink(new FairRootFileSink(source->ROOTFileName()));
  auto *unpackTask = new ETFUserUnpackTask();
  // unpackTask->SetDebug();
  run->AddTask(unpackTask); // add analysis task
  ETFUserAnaTask *anaTask = new ETFUserAnaTask();
  // anaTask->SetDetDebug(); anaTask->SetTrackingDebug(); anaTask->SetPIDDebug();
  run->AddTask(anaTask); // add analysis task
  /// analysis level: 0: only mapping; 1: 2D tracking; 2: PID;
  anaTask->SetConfigFolder("C16Jul2018");
  anaTask->SetChIdConfigFile("chIdMap.yaml");
  anaTask->SetDetPosConfigFile("detPos.yaml");
  anaTask->SetDelayConfigFile("delay.yaml");
  anaTask->SetMisConfigFile("mis.yaml"); // miscellaneous parameters
  // pion: 0.24835; C16: 1.45; C15: 1.295; C14: 1.20; C12: 0.9535
  // unit: Telsa     0-C16  1-C15  2-C14   3-C13, 4-C12   5-O18   6-O18-NOTA
  const double B[] = {1.45, 1.295, 1.2006, 1.075, 0.9535, 1.2004, 1.2336};
  anaTask->SetMagneticIntensity(B[1]);
  //////////   set analysis depth   //////////
  anaTask->SetAnaLevel(2);
  anaTask->SetIs3DTracking(true); // 3D tracking for postMagTrack
  // anaTask->AddDrawEvent({1,2,3,4,5,6,7,8,99}); // add events to be drawn
  run->Init();

  TStopwatch timer;
  // prepare and read the events
  timer.Start();
  run->Run(-1, 1000); // (ev_start, ev_stop): set ev_start<0 to run all the events
  timer.Stop();

  cout << endl << "Real time: " << timer.RealTime() << "s" << endl;
  cout << "CPU time: " << timer.CpuTime() << "s." << endl;
} // end the main function sim
